package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.FieldDesign;
import cn.wsn2018.tools.service.FieldsService;
import cn.wsn2018.tools.util.ServletUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
/**
 * 用于处理用户协议字段设计的控制类
 */
public class FieldsController {
    private Logger logger=Logger.getLogger(FieldsController.class);
    @Resource
    private FieldsService fieldsService;
    @RequestMapping(CommonValue.REQUEST_MAPPING_LISTFIELDS)
    /**
     * 处理用户选择的协议名，提供协议字段的罗列
     */
    public void listFields(HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String protocolName=request.getParameter(CommonValue.REQUEST_PARAM_PROTOCOLNAME);
        String json=fieldsService.dealFieldsQuery(protocolName);
        ServletUtil.getInstance().responseOperating(response,json);
    }
    @RequestMapping(CommonValue.REQUEST_MAPPING_LISTMODELS)
    /**
     * 罗列安全字段设计模板
     */
    public void listModels(HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String json=fieldsService.dealModelsQuery();
        ServletUtil.getInstance().responseOperating(response,json);
    }
    @RequestMapping(CommonValue.REQUEST_MAPPING_SAVEFIELDDESIGNS)
    /**
     * 保存用户设计的协议字段配置信息
     */
    public void saveFieldDesigns(HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String json=request.getParameter("json");
        //解析json格式的用户字段设计配置信息
        JSONObject data = JSONObject.fromObject(json);
        String username=data.optString(CommonValue.REQUEST_PARAM_USERNAME);
        String protocol=data.optString("protocol");
        JSONArray content = data.getJSONArray("content");
        List<FieldDesign> list=new ArrayList();
        for(int i=0;i<content.size();i++){
            String fields=content.getString(i);
            JSONObject designField=JSONObject.fromObject(fields);
            FieldDesign fieldDesign=new FieldDesign();
            fieldDesign.setType(designField.getString("type"));
            fieldDesign.setFieldName(designField.getString("name"));
            fieldDesign.setFieldType(designField.getString("data_type"));
            fieldDesign.setFieldLength(designField.getString("length"));
            fieldDesign.setFieldMeaning(designField.getString("meaning"));
            fieldDesign.setFieldStart(designField.getString("position_start"));
            fieldDesign.setFieldEnd(designField.getString("position_end"));
            fieldDesign.setInformation(designField.getString("information"));
            list.add(fieldDesign);
        }
        String respJson=fieldsService.dealSaveFieldDesigns(username,protocol,list);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @RequestMapping(CommonValue.REQUEST_MAPPING_REVIEWDESIGN)
    /**
     * 用于处理前端查看协议字段设计预览效果的方法
     */
    public void reviewDesign(HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String username=request.getParameter(CommonValue.REQUEST_PARAM_USERNAME);
        String protocolName=request.getParameter(CommonValue.REQUEST_PARAM_PROTOCOLNAME);
        String json=fieldsService.dealReviewProtocolDesign(username,protocolName);
        logger.debug(protocolName+":"+username);
        ServletUtil.getInstance().responseOperating(response,json);
    }
}
